package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.CheckGroupDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.CheckGroup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = CheckGroupService.class)
public class CheckGroupServiceImpl implements CheckGroupService {

    @Autowired
    private CheckGroupDao checkGroupDao;




    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        int i = 1/0;
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        Page<CheckGroup> page = checkGroupDao.findPage(queryPageBean);
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public CheckGroup findById(Integer id) {
        return checkGroupDao.findById(id);
    }

    @Transactional
    @Override
    public void add(CheckGroup checkGroup,Integer[] checkitemIds) {
        checkGroupDao.add(checkGroup);
        Integer id = checkGroup.getId();
        if (checkitemIds !=null && checkitemIds.length>0){
            for (Integer checkitemId : checkitemIds) {
                checkGroupDao.addgroup_item(id,checkitemId);
            }
        }
    }
    @Transactional
    @Override
    public void delete(Integer id) {
        checkGroupDao.deleteGroupItem(id);
        checkGroupDao.delete(id);
    }
    @Transactional
    @Override
    public void update(CheckGroup checkGroup,Integer[] checkitemIds) {
        checkGroupDao.deleteGroupItem(checkGroup.getId());
        checkGroupDao.update(checkGroup);
        if (checkitemIds !=null && checkitemIds.length>0){
            for (Integer checkitemId : checkitemIds) {
                checkGroupDao.addgroup_item(checkGroup.getId(),checkitemId);
            }
        }
    }

    @Override
    public Integer[] findItems(Integer id) {
        return checkGroupDao.findItemIds(id);
    }

    @Override
    public List<CheckGroup> findAll() {
        return checkGroupDao.findAll();
    }
}
